package com.hefu.virtuallife.ui.hotel;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.fei.root.recater.adapter.CommonAdapter;
import com.fei.root.recater.viewholder.CommonHolder;
import com.hefu.virtuallife.R;
import com.hefu.virtuallife.base.BaseActivity;
import com.hefu.virtuallife.ui.VoiceActivity;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class HotelIndexActivity extends VoiceActivity {


    @BindView(R.id.img_return)
    ImageView imgReturn;
    @BindView(R.id.tool_bar)
    Toolbar toolBar;
    @BindView(R.id.textTime)
    TextView textTime;
    @BindView(R.id.textSearch)
    TextView textSearch;
    @BindView(R.id.textNearBy)
    TextView textNearBy;
    @BindView(R.id.imgNearBy)
    ImageView imgNearBy;
    @BindView(R.id.textPrice)
    TextView textPrice;
    @BindView(R.id.imgPrice)
    ImageView imgPrice;
    @BindView(R.id.textOpnion)
    TextView textOpnion;
    @BindView(R.id.imgOPnion)
    ImageView imgOPnion;
    @BindView(R.id.list)
    RecyclerView list;

    private boolean dataChanged = false;
    private int[] rotates = new int[3];

    @Override
    protected void onResume() {
        super.onResume();
        showDialog();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_hotel_index;
    }

    @Override
    protected void initVariables(@Nullable Bundle savedInstanceState) {

        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("1");
        arrayList.add("1");
        CommonAdapter<String> adapter = new CommonAdapter<String>(arrayList, R.layout.layout_item_hotel) {
            @Override
            protected void convert(CommonHolder holder, String s, int position) {
            }
        };
        list.setLayoutManager(new LinearLayoutManager(this));
        list.setAdapter(adapter);
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH)+1;
        int date=calendar.get(Calendar.DATE);
        textTime.setText(month+"."+date+"-"+month+"."+date);
    }

    @OnClick(R.id.img_return)
    protected void onFinishClick() {
        finish();
    }

    @OnClick(R.id.textSearch)
    protected void onSearchClick() {
        if (!dataChanged){
            return;
        }
        CommonAdapter<String> adapter = (CommonAdapter<String>) list.getAdapter();
        if (adapter.getItemCount() > 3){
            adapter.removeItem(3);
        } else {
            adapter.appendItem("1");
        }
        dataChanged = false;
    }

    private void showDialog() {
        Dialog dialog = new Dialog(this, R.style.transDialog);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.layout_dialog_hotel);
        dialog.show();
    }

    @Override
    protected void uiProcess() {
        startActivity(new Intent(this, HotelResultActivity.class));
    }

    @OnClick(R.id.textTime)
    protected void onTimeclick() {
        DateRangePickerFragment dateRangePickerFragment = DateRangePickerFragment.newInstance(new DateRangePickerFragment.OnDateRangeSelectedListener() {
            @Override
            public void onDateRangeSelected(int startDay, int startMonth, int startYear, int endDay, int endMonth, int endYear) {
                textTime.setText(startMonth + 1 + "." + startDay + "-" + (endMonth + 1) + "." + endDay);
                dataChanged = true;
            }
        }, false);
        dateRangePickerFragment.show(getSupportFragmentManager(), null);
    }

    @OnClick(R.id.textNearBy)
    public void onTextNearByClicked() {
        rotates[0] = (rotates[0] + 180) % 360;
        imgNearBy.setRotation(rotates[0]);
    }

    @OnClick(R.id.textPrice)
    public void onTextPriceClicked() {
        rotates[1] = (rotates[1] + 180) % 360;
        imgPrice.setRotation(rotates[1]);

    }

    @OnClick(R.id.textOpnion)
    public void onTextOpnionClicked() {
        rotates[2] = (rotates[2] + 180) % 360;
        imgOPnion.setRotation(rotates[2]);

    }
}
